package com.stable.service;

import com.alibaba.fastjson.JSON;
import com.stable.constant.RedisConstant;
import com.stable.es.dao.base.EsProBarInfoDao;
import com.stable.spider.eastmoney.EastHsgtcgSpider;
import com.stable.spider.eastmoney.FundHoldGpSpider;
import com.stable.utils.RedisUtil;
import com.stable.vo.spi.req.EsQueryPageReq;
import com.stable.vo.spi.req.FundHoldGp;
import com.stable.vo.spi.req.FundInterest;
import com.stable.vo.spi.req.FundInterestGp;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.Semaphore;
import java.util.stream.Collectors;

@Service("FundInterestService")
@Log4j2
public class FundInterestService {
    @Autowired
    private EsProBarInfoDao esProBarInfoDao;

    @Autowired
    private EastHsgtcgSpider eastHsgtcgSpider;

    @Autowired
    private RedisUtil redisUtil;

    private final Semaphore semapProBar = new Semaphore(1);


    public List<FundInterest> queryList(String code, String name, EsQueryPageReq queryPage) {
        log.info("page code={},name={}", code, name);
        List<FundInterest> allList = new ArrayList<>();

        Map<Object, Object> guzhiMark = redisUtil.hGetAll(RedisConstant.fund_interest_map);
        for(Map.Entry<Object, Object> entry : guzhiMark.entrySet()){
            FundInterest redisData = JSON.parseObject((String) entry.getValue(), FundInterest.class);
            if (!Objects.isNull(redisData)) {
                FundInterest fundInterest = new FundInterest();
                fundInterest.setCode(redisData.getCode());
                fundInterest.setName(redisData.getName());
                fundInterest.setType(redisData.getType());
                fundInterest.setMark(redisData.getMark());
                allList.add(fundInterest);
            }
        }
        return allList.stream().sorted(Comparator.comparing(FundInterest::getCode)).collect(Collectors.toList());
    }

    public void editFundInterest(String code, FundInterest fundInterest) {
        redisUtil.hPut(RedisConstant.fund_interest_map, code, JSON.toJSONString(fundInterest));
    }

    public void delFundInterest(String code) {
        redisUtil.hDelete(RedisConstant.fund_interest_map, code);
    }

    public List<FundHoldGp> fundHoldGpQuery() {
        List<FundHoldGp> result = new ArrayList<>();

        List listAll = new ArrayList();
        Map<Object, Object> guzhiMark = redisUtil.hGetAll(RedisConstant.fund_interest_map);
        for(Map.Entry<Object, Object> entry : guzhiMark.entrySet()) {
            FundInterest redisData = JSON.parseObject((String) entry.getValue(), FundInterest.class);
            String jjcode = redisData.getCode();
            listAll.addAll(FundHoldGpSpider.getFundInterestGp(jjcode));
        }
        Map<String,ArrayList<FundInterestGp>> map = (Map) listAll.stream().collect(Collectors.groupingBy(FundInterestGp::getName, LinkedHashMap::new, Collectors.toList()));
        map.entrySet().stream().forEach(x -> {
            FundHoldGp fundHoldGp = new FundHoldGp();
            StringBuilder builder = new StringBuilder();
            x.getValue().forEach(y->{
                builder.append(y.getJjcode()+",");
            });
            fundHoldGp.setName(x.getKey());
            fundHoldGp.setCount(x.getValue().size());
            fundHoldGp.setFundList(builder.toString());
            result.add(fundHoldGp);
        });
        return result.stream().sorted(Comparator.comparing(FundHoldGp::getCount).reversed()).collect(Collectors.toList());
    }
}
